package com.my.meitianbackup.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgItemCouponGetRequest;
import com.taobao.api.request.TbkItemGetRequest;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.request.TbkUatmEventGetRequest;
import com.taobao.api.request.TbkUatmEventItemGetRequest;
import com.taobao.api.response.TbkDgItemCouponGetResponse;
import com.taobao.api.response.TbkItemGetResponse;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkUatmEventGetResponse;
import com.taobao.api.response.TbkUatmEventItemGetResponse;


public class TaokeUtils {
	private static Logger logger = LoggerFactory.getLogger("TaokeUtils");

	private static String url = "http://gw.api.taobao.com/router/rest";
	private static String appkey = "23412276";
	private static String secret = "a9debfb8097b629c1fcbca9051de4d80";
	private static Long adzoneId = 66428818L;
	private static Long keyiwoAdzoneId = 61680447L;
	

	List<String> urlList = new ArrayList<String>();

	public static String search(String keyword, long pageNum, long pageSize) {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkItemGetRequest req = new TbkItemGetRequest();
		req.setFields(
				"num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
		req.setQ(keyword);
		//req.setCat("16,18");
		//req.setSort("total_sales");
		req.setIsTmall(false);
		req.setIsOverseas(false);
		//req.setStartPrice(10L);
		req.setEndPrice(350L);
		//req.setStartTkRate(123L);
		req.setEndTkRate(500L);
		req.setPlatform(1L);
		req.setPageNo(pageNum);
		req.setPageSize(pageSize);
		TbkItemGetResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			logger.debug(e.getMessage());
			e.printStackTrace();
		}
		return rsp.getBody();
	}
	
	public static void dingXiangList() {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkUatmEventGetRequest req = new TbkUatmEventGetRequest();
		req.setPageNo(1L);
		req.setPageSize(20L);
		req.setFields("event_id,event_title,start_time,end_time");
		TbkUatmEventGetResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(rsp.getBody());
	}
	
	public static void dingXiangDetail() {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkUatmEventItemGetRequest req = new TbkUatmEventItemGetRequest();
		req.setPlatform(1L);
		req.setPageSize(100L);
		req.setAdzoneId(adzoneId);
		req.setUnid("3456");
		req.setEventId(123456L);
		req.setPageNo(1L);
		req.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,type,status");
		TbkUatmEventItemGetResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(rsp.getBody());
	}
	
	public static void youhuiquan() {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();
		req.setAdzoneId(keyiwoAdzoneId);
		req.setPlatform(2L);
		//req.setCat("16,18");
		req.setPageSize(100L);
		req.setQ("女装");
		req.setPageNo(1L);
		TbkDgItemCouponGetResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(rsp.getBody());
	}
	
	public static void main(String[] args) {
		youhuiquan();
	}
	
	/*public void danpinquan() {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkItemCouponGetRequest req = new TbkItemCouponGetRequest();
		req.setPlatform(1L);
		req.setCat("16,18");
		req.setPageSize(1L);
		req.setQ("女装");
		req.setPageNo(1L);
		req.setPid("mm_123_123_123");
		TbkItemCouponGetResponse rsp = client.execute(req);
		System.out.println(rsp.getBody());
	}*/
	
	public static String getDetail(String id) {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
		req.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url");
		req.setPlatform(1L);
		req.setNumIids(id);
		TbkItemInfoGetResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rsp.getBody();
	}
	
	public static void getImgs() {
		List<Record> list = Db.find("select * from meitian where hasInfo = 1 and hasImgs = 0");
		int index = 0;
		for(int i=0; i<list.size(); i++) {
			String numiid = list.get(i).getLong("id")+"";
			List<Record> result = Db.find("select * from goodImgs where numiid = "+ numiid);
			if(result != null && result.size()>0) {
				//System.out.println(result);
				System.out.println("continue:"+numiid);
				continue;
			}
			try {
				String dd = getDetail(numiid);
				System.out.println("data:"+dd);
				JSONObject jb = new JSONObject(dd);
				JSONArray ja = jb.getJSONObject("tbk_item_info_get_response").getJSONObject("results").getJSONArray("n_tbk_item").getJSONObject(0).getJSONObject("small_images").getJSONArray("string");
				List<Record> rl = new ArrayList<Record>();
				for(int j=0; j<ja.length(); j++) {
					Record record = new Record();
					record.set("numiid", numiid);
					record.set("imgUrl", ja.getString(j));
					rl.add(record);
				}
				if(rl.size()>=4) {
					int[] ii = Db.batchSave("goodImgs", rl, rl.size());
					int iii = Db.update("update meitian set hasImgs = 1 where id = ?", numiid);
					index = index + iii;
					System.out.println("ii: "+Arrays.toString(ii) + "-- index:"+index);
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("exception:"+numiid);
			}
		}
	}

}
